//pages/productDetail/productDetail.js
var app = getApp(); 
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据    
   */
  data: {
    haveVideo:true,
    headBtnIndex:0,
    headBtn:['视频','图片'],
    readPercent: 0,
    actionSheetHidden: true,
  },
  headBtnChoose:function(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      headBtnIndex:index,
    })
  },
  joinUs: function (e) {
    var that = this;
    var source_txt = "来自产品" + that.data.detail.goods_name
    wx.navigateTo({
      url: '/pages/joinUs/joinUs?source=product&source_txt=' + source_txt,
    })
  }, 
  makePhone: function (e) {
    app.userBehavior(116,'电话咨询产品《'+this.data.detail.goods_name+"》")
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  // 留言
  tochat: function () {
    if(!wx.getStorageSync('userInfo')){
      wx.navigateTo({
        url: '/pages/welcome/welcome',
      })
      return;
    }
    var that = this;
    var product_name = that.data.detail.goods_name;
    var filename = that.data.detail.image[0].file_path;
    app.userBehavior(117,'在线咨询产品《'+that.data.detail.goods_name+"》")
    wx.navigateTo({
      url: '/pages/chat/chat?content=' + product_name + "&filename=" + filename,
    })
  },
  // 点击分享
  actionSheetTap: function () {
    var that = this;
    that.setData({
      actionSheetHidden: !that.data.actionSheetHidden
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu1: function () {
    this.setData({
      menu: 1,
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu2: function (e) {
    var that = this;
    this.setData({
      menu: 2,
      actionSheetHidden: !this.data.actionSheetHidden
    })
    console.log(that.data.id)
    wx.navigateTo({
      url: '/pages/productCard/index?source=product&productid='+that.data.id,
    })
  },
  // 滚动计算阅读
  scroll: function (e) {
    var that = this;
    var scrollHeight = e.detail.scrollHeight;
    var scrollTop = e.detail.scrollTop;
    var windowHeight = that.data.windowHeight;
    var height = scrollHeight - windowHeight - 20;
    var scrollPercent = (scrollTop / height) * 100;
    scrollPercent = Math.ceil(scrollPercent);
    var readPercent = that.data.readPercent;
    if (scrollPercent > 100) {
      scrollPercent = 100;
      readPercent = 100;
    } else {
      scrollPercent = scrollPercent;
      if (scrollPercent > readPercent) {
        readPercent = scrollPercent;
      } else {
        readPercent = readPercent;
      }
    }
    that.setData({
      readPercent: readPercent
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    app.httpRequest('goods/detail', {goods_id: id}, 'get', false, function(res){
      var article = res.data.detail.content
      WxParse.wxParse('article', 'html', article, that, 0);
      res.data.detail.content = article;
      that.setData({
        detail: res.data.detail,
        card: res.data.card,
        pageShow: true,
      })
      wx.setNavigationBarTitle({
        title: res.data.detail.goods_name,
      })
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var startTime = new Date().getTime();
    that.setData({
      startTime: startTime,
      windowHeight: wx.getStorageSync('systemInfo').windowHeight
    })
  },
  // 获得停留时间，毫秒级
  getStopTime: function () {
    var that = this;
    var endTime = new Date().getTime();
    var startTime = that.data.startTime;
    var stopTime = endTime - startTime;
    that.setData({
      stopTime: stopTime
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(115,'浏览产品《'+that.data.detail.goods_name+"》",stoptime,percent)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(115,'浏览产品《'+that.data.detail.goods_name+"》",stoptime,percent)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    app.userBehavior(114,'分享产品《'+that.data.detail.goods_name+"》")
    return {
      title: that.data.detail.goods_name,
      path: "/pages/productDetail/productDetail?share_card_id=" + wx.getStorageSync('share_card_id') + "&share_user_id=" + wx.getStorageSync('user_id') + '&id=' + that.data.detail.goods_id,
      success: function (a) {
        
      },
      fail: function (a) { }
    };
  }
})